using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.OracleClient;
using System.Collections.Generic;
using Pms.Utils;
using Biblioteca.Utils;
using Biblioteca.DataBase;
using SERIX.Util;
/*
 * 
 * Autor      : Jose Jornando de Carvalho Junior    
 * Data       : 10/04/2010 07:08:35
 * Descricao  : 
 * Alteracoes : 
 * 
 */

namespace SERIX
{
   public class CTerritorios
   {
       private string IDT_TER ;
       private string DSC_TER ;

	   /// <sumary>
       /// Construtor da classe territorios
       /// <sumary>
       public CTerritorios()
       {
   
       }

	   /// <sumary>
       /// Construtor da classe Territorios
       /// <sumary>
       public CTerritorios( string IDT_TER, string DSC_TER )
       {
          this.IDT_TER = IDT_TER;
          this.DSC_TER = DSC_TER;
       }
	   
	   #region Propriedades
     	/// <sumary>
        /// Identificador do territorio
        /// <sumary>
        public string Idt_ter 
		{
		   get
		   {
		       return this.IDT_TER;
		   }
		   set
		   {
		       this.IDT_TER = value;
		   }
		}
     	/// <sumary>
        /// Descricao do territorio
        /// <sumary>
        public string Dsc_ter 
		{
		   get
		   {
		       return this.DSC_TER;
		   }
		   set
		   {
		       this.DSC_TER = value;
		   }
		}
	    #endregion Propriedades
	
	    /// <summary>
        /// Inserindo um novo Territorios.
        /// </summary>
        public void Inserir(string Login, string tab)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"insert into Serix.SRX_TERRITORIOS"+tab+@"
			                  ( IDT_TER, DSC_TER )
						   values
						      ( :IDT_TER, :DSC_TER )";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("IDT_TER",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("DSC_TER",OracleType.VarChar)); 
				
                command.Parameters["IDT_TER"].Value = this.IDT_TER;
                command.Parameters["DSC_TER"].Value = this.DSC_TER;
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	    /// <summary>
        /// Alterando um Territorios.
        /// </summary>
        public void Alterar(string Login, string tab)
        {
		    OracleCommand command = new OracleCommand();
            OracleTransaction trans;

            string SQL = @"update Serix.SRX_TERRITORIOS" + tab + @" set 
			                   DSC_TER = :DSC_TER
						   where
						       IDT_TER = :IDT_TER ";
     		
			try
			{	
				command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login, BiblioPMS.Demo);
                trans = command.Connection.BeginTransaction();
                command.Transaction = trans;
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;
				
				command.Parameters.Add(new OracleParameter("IDT_TER",OracleType.Number)); 
				command.Parameters.Add(new OracleParameter("DSC_TER",OracleType.VarChar)); 
				
                command.Parameters["IDT_TER"].Value = this.IDT_TER;
                command.Parameters["DSC_TER"].Value = this.DSC_TER;
				command.ExecuteNonQuery();
                command.Parameters.Clear();
                command.Transaction.Commit();
            }
            catch (Exception e)
            {
                command.Transaction.Rollback();

                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }
		}
		
	     /// <summary>
        /// Exclui um  Territorios.
        /// </summary>
        public void Excluir(string Login, string tabs)
        {
            Tabela tab = new Tabela("SRX_TERRITORIOS"+tabs, BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, Login,BiblioPMS.Demo));
            tab.adicionarFiltro(new Coluna("IDT_TER", DbType.Int32, this.IDT_TER));
            tab.delete();
        }
		
		static public DataTable ObterTodos(string login,string tab)
        {
            
            OracleCommand command = new OracleCommand();

            string SQL = @"select * from Serix.SRX_TERRITORIOS" + tab + @" order by DSC_TER ";

            try
            {
                command.Connection = BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, false);
                command.CommandType = CommandType.Text;
                command.CommandText = SQL;

                DataTable tbl = new DataTable();
                tbl.Load(command.ExecuteReader(CommandBehavior.CloseConnection)); 
                return tbl; 
                
            }
            catch (Exception e)
            {
 
                if (e is OracleException)
                    throw new Exception(Biblio.ObterMensagem(((OracleException)e).Code, e.Message));
                else
                    throw new Exception(e.Message + " : " + "");
            }

        }
		
		static public CTerritorios PesquisaporCodigo(string idt_ter, string login, string tab)
        {
            CTerritorios obj;
            string SQL = @"SELECT *
                            FROM  Serix.SRX_TERRITORIOS" + tab + @" 
                            WHERE IDT_TER = :IDT_TER";
            Query qy = null;
            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
				qy.AdicionarParametro(":IDT_TER", DbType.String, idt_ter);
               
				qy.Executar();
                obj = new CTerritorios( qy.ObterCampo("IDT_TER"), qy.ObterCampo("DSC_TER") );
            }                                  
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return new CTerritorios();
            }
			finally
            {
                qy.FecharConexao();
            }
            return obj;
        }
		
		static public int ObterNovoIdt(string login, string tab)
        {            
            OracleConnection con = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            string sql = @"SELECT COUNT(IDT_TER) FROM SRX_TERRITORIOS" + tab;
            Query qy = new Query(con, sql);
            int retorno = -1;

            try
            {
                con.Open();
                qy.Executar();
                retorno = Convert.ToInt32(qy.ObterCampo("COUNT(IDT_TER)")) +1;                
            }
            catch { retorno = -1; }
            finally
            { 
                con.Close();
                
            }
            return retorno;

        }

        static public DataTable ObterDescricoes(string tab, string login)
        {
            string SQL = "SELECT DSC_TER FROM SRX_TERRITORIOS"+tab;
            Query qy = null;
            DataTable dt = null;

            try
            {
                qy = new Query(BiblioPMS.ObterConexao(BiblioPMS.aplicacaoSNG, login, BiblioPMS.Demo), SQL);
                qy.Executar();
                dt = qy.ObterDataTable();
                return dt;
            }
            catch
            {
                if (qy.EstadoConexao() == ConnectionState.Open)
                {
                    qy.FecharConexao();
                }
                return null;
            }
            finally
            {
                qy.FecharConexao();                
            }
        }
    }
}	

